import * as dutyNotesService from '@/services/dutyNotes';
import { message } from 'antd';
import { DEFAULT_RESPONE_SUCCESS } from '@/constants';

export default {
  namespace: 'dutyNotes',

  state: {
    current: 1,
    total: 0,
    data: [],
    rowKey: 'id',
  },

  subscriptions: {},

  effects: {
    *queryData({ payload }, { call, put }) {
      const { callback, current, pageSize, ...extra } = payload;
      const response = yield call(
        dutyNotesService.queryData,
        { pageNo: current, pageSize },
        { ...extra.remember },
      );
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = {} } = response;
        const { content = [], totalElements: total = 0 } = result;

        yield put({
          type: 'changeState',
          payload: {
            data: content,
            current,
            pageSize,
            total: typeof total === 'string' ? parseInt(total) : total,
          },
        });
      }
    },

    // 查询
    *queryData2({ payload }, { call, put }) {
      const { ...extra } = payload;
      const response = yield call(
        dutyNotesService.queryData,
        { pageNo: extra.extra.current, pageSize: extra.extra.pageSize },
        extra.extra.remember,
      );
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = {} } = response;
        const { content = [], totalElements: total = 0 } = result;

        yield put({
          type: 'changeState',
          payload: {
            data: content,
            current: extra.extra.current,
            pageSize: extra.extra.pageSize,
            total: typeof total === 'string' ? parseInt(total) : total,
          },
        });
      }
    },

    // 删除
    *deleteData({ payload }, { call, put }) {
      try {
        const { ...extra } = payload;
        const option = {
          method: 'POST',
          body: extra.list,
        };
        const res = yield call(dutyNotesService.deleteData, option);
        if (res) {
          message.success('删除成功');
        }
        // 刷新列表
        yield put({ type: 'queryData2', payload: { extra } });
      } catch (err) {
        message.error('删除失败');
        console.log('err', err);
      }
    },

    //值班抽查
    *spotcheckduty({ payload }, { call, put }) {
      const response = yield call(dutyNotesService.callSelect, { method: 'POST' });
    },

    //催告
    *noticeData({ payload }, { call, put }) {
      try {
        const { list } = payload;
        const option = {
          method: 'POST',
          body: list,
        };
        const res = yield call(dutyNotesService.noticeData, option);
        if (res) {
          message.success('催告成功');
        }
      } catch (err) {
        message.error('催告失败');
        console.log('err', err);
      }
    },

    // 编辑或添加
    *saveOrUpdateData({ payload }, { call, put }) {
      const { data, ...extra } = payload;
      const option = {
        method: 'POST',
        body: [data],
      };

      let response = {};
      if (!data.id) {
        response = yield call(dutyNotesService.saveData, option);
      } else {
        response = yield call(dutyNotesService.updateData, option);
      }
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        message.success(!data.id ? '新增突发事件成功' : '编辑突发事件成功', 1);
        yield put({
          type: 'changeState',
          payload: { visible: false, editData: {} },
        });
        yield put({ type: 'queryData2', payload: { extra } });
      }
    },

    // 下载
    *downloadData({ payload }, { call, put }) {
      const { callback, ...extra } = payload;
      const { remember } = extra;
      const { RememberID, eventID, period } = remember;

      const response = yield call(dutyNotesService.downloadData, { RememberID, eventID, period });
      if (response) {
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(response, name);
        } else {
          const URL = window.URL || window.webkitURL;
          const downloadUrl = URL.createObjectURL(response);
          const a = document.createElement('a');

          if (typeof a.download === 'undefined') {
            window.location = downloadUrl;
          } else {
            a.download = '突发事件.doc';
            a.href = downloadUrl;
            a.style.display = 'none';
            document.body.appendChild(a);
            a.click();
            window.URL.revokeObjectURL(downloadUrl);
            document.body.removeChild(a);
          }
        }
        if (typeof callback === 'function') {
          callback();
        }
      } else {
        message.error('下载失败！');
      }
    },
  },

  reducers: {
    clear(state, { payload }) {
      return {
        current: 1,
        total: 0,
        data: [],
        rowKey: 'id',
      };
    },
    changeState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
